package com.huobao.yunying.utils;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.entity.Wuliu;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.dialog.MyDialog;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class DialogUtils {
	private static Dialog dialog;

	private static void createDialog(Context ctx) {
		dialog = new MyDialog(ctx, R.style.dialogdialog);
		// dialog.setCanceledOnTouchOutside(false);
		Window window = dialog.getWindow();
		window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置
		window.setWindowAnimations(R.style.dialoganimationstyle); // 添加动画
	}

	/**
	 * 跳页的dialog
	 */

	public static void ShowTiaoYeDialog(Context context, final DialogListener Listener) {
		if (dialog != null) {
			return;
		}
		createDialog(context);
		View view = LayoutInflater.from(context).inflate(R.layout.layout_tiaoye_dialog, null);
		dialog.setContentView(view);
		Button Btn_Submit = (Button) view.findViewById(R.id.btn_submit);
		Button Btn_Cancle = (Button) view.findViewById(R.id.btn_cancle);
		final EditText meditText = (EditText) view.findViewById(R.id.edit_num);
		Btn_Submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				dialog = null;
				String msg = meditText.getText().toString();
				Listener.OnSubmit(msg);
			}
		});

		Btn_Cancle.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				dialog = null;
				Listener.OnCancle();
			}
		});
		dialog.show();
	}

	public interface DialogListener {
		void OnSubmit(String msg);

		void OnCancle();
	}

	/**
	 * 取消订单的dialog
	 *
	 * @param context
	 * @param msg
	 * @param listener
	 */
	public static void showCancelOrderDialog(final Context context, String msg,
											 final onDialogDiamissListener listener) {
		if (dialog != null)
			return;
		createDialog(context);
		View view = LayoutInflater.from(context).inflate(R.layout.layout_cancelorder_dialog, null);
		TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
		TextView tv_close = (TextView) view.findViewById(R.id.tv_close);
		tv_close.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				dialog = null;
			}
		});
		Button btn_1 = (Button) view.findViewById(R.id.btn_1);
		tv_content.setText(msg);
		dialog.setContentView(view);
		btn_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				dialog = null;
				listener.onDialogDismiss();
			}
		});
		dialog.show();
	}



	/**
	 * 发货和修改单号
	 *
	 * @param context
	 * @param dingdanhao
	 */
	public static void showEditDanhaoDialog(final boolean isFhuo, final Context context, String dingdanhao,String wuliu,String danhao,
											final onDialogDiamissListener listener) {
		if (dialog != null)
			return;
		createDialog(context);
		View view = LayoutInflater.from(context).inflate(R.layout.view_edit_danhao_dialog, null);
		TextView tv_dingdanhao = (TextView) view.findViewById(R.id.tv_dingdanhao);
		TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
		tv_title.setText(isFhuo ? "发货" : "修改单号");
		tv_dingdanhao.setText("订单号：" + dingdanhao);
		final TextView tv_wuliu = (TextView) view.findViewById(R.id.tv_wuliu);
		tv_wuliu.setText(isFhuo?"请选择物流公司":wuliu);
		final EditText edt_danhao = (EditText) view.findViewById(R.id.edt_danhao);
		edt_danhao.setText(isFhuo?"请选择物流公司":danhao);
		final EditText edt_yuanyin = (EditText) view.findViewById(R.id.edt_yuanyin);
		LayoutParams layoutParams = (LayoutParams) edt_yuanyin.getLayoutParams();
		layoutParams.width = ScreenUtil.getInstant().getDisWidth(context) - 50;
		edt_yuanyin.setLayoutParams(layoutParams);
		Button btn_1 = (Button) view.findViewById(R.id.btn_1);
		Button btn_2 = (Button) view.findViewById(R.id.btn_2);
		TextView tv_close = (TextView) view.findViewById(R.id.tv_close);
		tv_close.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				dialog = null;
			}
		});
		tv_wuliu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				Map<String, String> map = HttpUtil.getParams("app", "sellerorder", "act", "shipped_store");
				Http.getInstant().sendRequest(context, map, new HttpListener() {

					@Override
					public void onHttpSuccess(String result) {
						JSONObject jsonObject = JSON.parseObject(result);
						final List<Wuliu> list = JSON.parseArray(jsonObject.getString("retval"), Wuliu.class);
						PopupWuliu.showPop(context, list, v, new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
								PopupWuliu.dismiss();
								tv_wuliu.setText(list.get(position).name);
								tv_wuliu.setTag(list.get(position).value);
							}
						});
					}

					@Override
					public void onHttpError(String error) {

					}
				});
			}
		});
		btn_2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				dialog = null;
			}
		});
		btn_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (tv_wuliu.getTag() == null) {
					Toast.makeText(context, "请选择物流公司", Toast.LENGTH_SHORT).show();
					return;
				}
				String danhao = edt_danhao.getText().toString().trim();
				String yunyin = edt_yuanyin.getText().toString().trim();
				if (danhao.equals("")) {
					Toast.makeText(context, "请填写单号", Toast.LENGTH_SHORT).show();
					return;
				}
				dialog.dismiss();
				dialog = null;
				String wuliu = tv_wuliu.getTag().toString();
				listener.onDialogDismiss(danhao, yunyin, wuliu);
			}
		});
		dialog.setContentView(view);
		dialog.show();
	}

	public static interface onDialogDiamissListener {
		void onDialogDismiss(String... strings);
	}

	public static void showEditPriceDialog(final Context context, final String order_sn, final double order_amount,
										   final double shipping_fee, final onDialogDiamissListener lis) {
		if (dialog != null)
			return;
		createDialog(context);
		View view = LayoutInflater.from(context).inflate(R.layout.view_edit_price_dialog, null);
		TextView tv_close = (TextView) view.findViewById(R.id.tv_close);
		// 买家
		TextView tv_maijia = (TextView) view.findViewById(R.id.tv_maijia);
		// 订单号
		TextView tv_dingdanhao = (TextView) view.findViewById(R.id.tv_dingdanhao);
		tv_dingdanhao.setText("订单号：" + order_sn);
		// 商品总价
		TextView tv_allprice = (TextView) view.findViewById(R.id.tv_allprice);
		tv_allprice.setText("商品总价：￥" + Utils.get2point((order_amount - shipping_fee)));
		final EditText edt_allprice = (EditText) view.findViewById(R.id.edt_allprice);
		// 配送费用
		TextView tv_peisong_price = (TextView) view.findViewById(R.id.tv_peisong_price);
		tv_peisong_price.setText("配送费用：￥" + Utils.get2point(shipping_fee));
		final EditText edt_peisong = (EditText) view.findViewById(R.id.edt_peisong);
		// 订单总价
		TextView tv_zongjia = (TextView) view.findViewById(R.id.tv_zongjia);
		tv_zongjia.setText("订单总价：￥" + Utils.get2point(order_amount));
		Button btn_1 = (Button) view.findViewById(R.id.btn_1);
		Button btn_2 = (Button) view.findViewById(R.id.btn_2);
		tv_close.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				dialog = null;
			}
		});
		btn_2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				dialog = null;
			}
		});
		btn_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String shangpinPrice = edt_allprice.getText().toString().trim();
				String peisongPrice = edt_peisong.getText().toString().trim();
				if (shangpinPrice.equals("")) {
					Toast.makeText(context, "请填写商品总价", Toast.LENGTH_SHORT).show();
					return;
				}
				if (peisongPrice.equals("")) {
					Toast.makeText(context, "请填写配送费用", Toast.LENGTH_SHORT).show();
					return;
				}
				dialog.dismiss();
				dialog = null;
				lis.onDialogDismiss(shangpinPrice, peisongPrice);
			}
		});
		dialog.setContentView(view);
		dialog.show();
	}

	public static void showReceiverHuokuan(final Context context, final String order_sn,
										   final onDialogDiamissListener lis) {
		if (dialog != null)
			return;
		createDialog(context);
		View view = LayoutInflater.from(context).inflate(R.layout.view_receiver_price_dialog, null);
		TextView tv_close = (TextView) view.findViewById(R.id.tv_close);
		TextView tv_dingdanhao = (TextView) view.findViewById(R.id.tv_dingdanhao);
		tv_dingdanhao.setText("订单号：" + order_sn);
		final EditText edt_content = (EditText) view.findViewById(R.id.edt_content);
		tv_close.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				dialog = null;
			}
		});
		Button btn_1 = (Button) view.findViewById(R.id.btn_1);
		Button btn_2 = (Button) view.findViewById(R.id.btn_2);
		tv_close.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				dialog = null;
			}
		});
		btn_2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				dialog = null;
			}
		});
		btn_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String content = edt_content.getText().toString().trim();
				dialog.dismiss();
				dialog = null;
				lis.onDialogDismiss(content);
			}
		});
		dialog.setContentView(view);
		dialog.show();
	}
}
